package com.zzzzzz.content.topic;

import java.io.IOException;
import java.util.Locale;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;

import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.authz.annotation.RequiresUser;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.zzzzzz.account.signin.NoSuchOnlineUserException;
import com.zzzzzz.account.signin.OnlineService;
import com.zzzzzz.account.user.UserBase;
import com.zzzzzz.common.message.Message;
import com.zzzzzz.common.message.MessageIcon;
import com.zzzzzz.core.web.BaseController;

@Controller
public class TopicController extends BaseController {
	@Resource
	private TopicService topicService;
	@Resource
	private OnlineService onlineService;
	
	@RequiresUser
	@RequestMapping(value = "/content/topic/add", method = RequestMethod.GET)
	public String addPage(@ModelAttribute TopicForm topicForm) {
		return "content/topic/add";
	}

	@RequiresUser
	@RequestMapping(value = "/content/topic/add", method = RequestMethod.POST)
	public String add(Model model, @Valid TopicForm topicForm, BindingResult bindingResult, RedirectAttributes redirectAttributes, Locale locale) throws NoSuchOnlineUserException {
		if (bindingResult.hasErrors()) {
			return addPage(topicForm);
		}
		
		UserBase userBase = onlineService.findUserBase();
		try {
			String topicId = topicService.add(userBase, topicForm);
			return "redirect:/content/topic/" + topicId;
		} catch (IOException e) {
			redirectAttributes.addFlashAttribute(Message.error(getMessage("message.error.form.content.topic.add.io", locale), MessageIcon.WARNING));
			return "redirect:/content/topic/add";
		}
	}

	@RequiresUser
	@RequestMapping(value="/content/topic/edit/{topicId}", method=RequestMethod.GET)
	public String editPage(@PathVariable String topicId, @ModelAttribute TopicForm topicForm, Model model) throws NoSuchTopicException, NoSuchOnlineUserException, UserPermissionException {
		Topic topic = topicService.findById(topicId);
		
		String userId = onlineService.findUserId();
		if(!StringUtils.equalsIgnoreCase(topic.getUserId(), userId)) {
			throw new UserPermissionException();
		}
		
		topicForm.updateTopicForm(topic);
		
		model.addAttribute("topicId", topicId);
		model.addAttribute("imgSId", topic.getImgSId());
		
		return "content/topic/edit";
	}
	
	@RequiresUser
	@RequestMapping(value = "/content/topic/edit/{topicId}", method=RequestMethod.PUT)
	public String edit(@PathVariable String topicId, @Valid TopicForm topicForm, Model model, BindingResult result, RedirectAttributes redirectAttributes, Locale locale) throws NoSuchOnlineUserException {
		String userId = onlineService.findUserId();
		try {
			topicService.edit(userId, topicId, topicForm);
			redirectAttributes.addFlashAttribute(Message.success(getMessage("message.success.form.edit.submit", locale), MessageIcon.OK));
			return "redirect:/content/topic/" + topicId;
		} catch (IOException e) {
			redirectAttributes.addFlashAttribute(Message.error(getMessage("message.error.form.content.topic.add.io", locale), MessageIcon.WARNING));
			return "redirect:/content/topic/edit" + topicId;
		}
	}

	@RequestMapping(value="/content/topic/delete/{id}", method=RequestMethod.DELETE)
	public String deleteTopic(@PathVariable String id, Model model, HttpServletRequest request) throws Exception {
		String userId = onlineService.findUserId();
		topicService.editStatus(userId, id, 1);
		return "redirect:/";
	}
	
	@RequestMapping(value="/content/topic/view/{id}", method=RequestMethod.GET)
	public String view(@PathVariable String id, Model model) throws Exception {
		model.addAttribute("topicId", id);
		return "content/topic/view";
	}

	@RequestMapping(value="/content/topic", method=RequestMethod.GET)
	public @ResponseBody Topic getTopic(@RequestParam String topicId) throws Exception {
		Topic topic = topicService.findById(topicId);
		return topic;
	}
}
